<?php
namespace app\index\controller;
// use think\Db;
class Phpexcel extends Base{
    // public function phpexcel(){
    //     if(request()->isGet()){
    //     	return $this->fetch();
    //     }
    //     if(request() -> isPost())
    //     {
    //         $file = request()->file('excel');
    //     	//将文件保存到public/uploads目录下面
	   //      $info = $file->validate(['size'=>1048576,'ext'=>'xls,xlsx'])->move( './uploads');
	   //      if($info){
	   //          //获取上传到后台的文件名
	   //          $fileName = $info->getSaveName();
	   //          //获取文件路径
	   //          $filePath = "..".DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$fileName;
	   //          //获取文件后缀
	   //          $suffix = $info->getExtension();
	   //          //判断哪种类型
	   //          if($suffix=="xlsx"){
	   //              $reader = \PHPExcel_IOFactory::createReader('Excel2007');
	   //          }else{
	   //              $reader = \PHPExcel_IOFactory::createReader('Excel5');
	   //          }
	   //      }else{
	   //          $this->error('文件过大或格式不正确导致上传失败-_-!');
	   //      }
	   //      //载入excel文件
	   //      $excel = $reader->load("$filePath",$encode = 'utf-8');
	   //      //读取第一张表
	   //      $sheet = $excel->getSheet(0);
	   //      //获取总行数
	   //      $row_num = $sheet->getHighestRow();
	   //      //获取总列数
	   //      $col_num = $sheet->getHighestColumn();
	   //      $data = []; //数组形式获取表格数据
	   //      for ($i = 1; $i <= $row_num; $i ++) {
	   //          $data[$i]['code']  = $sheet->getCell("A".$i)->getValue();
	   //          $data[$i]['city']  = $sheet->getCell("B".$i)->getValue();
	   //          //将数据保存到数据库
	   //      }
	   //      Db::name('mspcitycode')->insertAll($data);
    //     }
    //     return $this->fetch();
    // }
    public function bank() {
    	$result=Db::name('bank')->select();
	    $xlsCell  = array(
	        array('id','ID'),
	        array('bank','bank'),
	        array('bank_code','bank_code'),
	        array('status','status'),
	        array('bank_cd','bank_cd')
	    );
    	$this->exportexcel('富友银行支持',$xlsCell,$result);
    }
    public function users() {
    	$result=Db::name('users')->select();
	    $xlsCell  = array(
	        array('id','id'),
	        array('cus_id','cus_id'),
	        array('name','name'),
	        array('phone','phone'),
	        array('password','password'),
	        array('idcard','idcard'),
	        array('idcard_img','idcard_img'),
	        array('idcard_imgback','idcard_imgback'),
	        array('img','img'),
	        array('address','address'),
	        array('address_detail','address_detail'),
	        array('email','email'),
	        array('wechat','wechat'),
	        array('qq','qq'),
	        array('relatives','relatives'),
	        array('relatives_phone','relatives_phone'),
	        array('partner','partner'),
	        array('partner_phone','partner_phone'),
	        array('friend','friend'),
	        array('friend_phone','friend_phone'),
	        array('emer_con','emer_con'),
	        array('emer_con_phone','emer_con_phone'),
	        array('company_name','company_name'),
	        array('company_address','company_address'),
	        array('industry','industry'),
	        array('work_age','work_age'),
	        array('time','time'),
	        array('uptime','uptime'),
	        array('status','status'),
	        array('login_time','login_time'),
	        array('bank_name','bank_name'),
	        array('bank_idcard','bank_idcard'),
	        array('bank_cdid','bank_cdid'),
	        array('bank_number','bank_number'),
	        array('bank_phone','bank_phone'),
	        array('task_id','task_id'),
	        array('yys_orderno','yys_orderno'),
	        array('yys_state','yys_state'),
	        array('other','other'),
	        array('mchntssn','mchntssn'),
	        array('protocolno','protocolno'),
	        array('invitephone','invitephone'),
	        array('sesame_state','sesame_state'),
	        array('sesame_score','sesame_score'),
	        array('yystime','yystime'),
	        array('appname','appname')
	    );
    	$this->exportexcel('用户信息',$xlsCell,$result);
    }
    public static function exportexcel($expTitle, $expCellName, $expTableData) {
	    $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
	    $fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
	    $cellNum = count($expCellName);
	    $dataNum = count($expTableData);

	    $objPHPExcel = new \PHPExcel();
	    $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
	    // Miscellaneous glyphs, UTF-8
	    for ($i = 0; $i < $dataNum; $i++) {
	        for ($j = 0; $j < $cellNum; $j++) {
	            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 1), $expTableData[$i][$expCellName[$j][0]]);
	        }
	    }
	    header('pragma:public');
	    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
	    header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
	    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	    $objWriter->save('php://output');
	    exit;
	}
}
